<template>
  <div id="home">
    <nav-bar class="nav-bar">
      <div slot="center">蘑菇街</div>
    </nav-bar>
    <tab-control
      :titles="['流行', '新款', '精选']"
      @itemClick="tabClick"
      ref="tabControl1"
      class="tab-control"
      v-show="isTabFixed"
    ></tab-control>
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      :pull-up-load="true"
      @scroll="contentScroll"
      @pullingUp="loadMore"
    >
      <home-swiper :banner="banner"></home-swiper>
      <feature-view :recommends="recommends"></feature-view>
      <recommend-view></recommend-view>
      <tab-control
        :titles="['流行', '新款', '精选']"
        @itemClick="tabClick"
        ref="tabControl2"
      ></tab-control>
      <goods-list :goods-list="showGoodsList"></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import HomeSwiper from "./childComps/HomeSwiper.vue";
import FeatureView from "./childComps/FeatureView.vue";
import RecommendView from "./childComps/RecommendView.vue";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";
import Scroll from "components/common/scroll/Scroll";
import BackTop from "components/content/backTop/BackTop";

import { getHomeMultidata, BANNER, RECOMMEND, getHomeData } from "network/home";
import { NEW, POP, SELL, BACKTOP_DISTANCE } from "@/common/const";
import { debounce } from "../../common/utils";

export default {
  name: "Home",
  components: {
    NavBar,
    HomeSwiper,
    FeatureView,
    RecommendView,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },
  data() {
    return {
      banner: [],
      recommends: [],
      goodsList: {
        pop: { page: 1, list: [] },
        new: { page: 1, list: [] },
        sell: { page: 1, list: [] },
      },
      currentType: POP,
      isShowBackTop: false,
      isTabFixed: false,
      tabOffsetTop: 0,
    };
  },
  computed: {
    showGoodsList() {
      return this.goodsList[this.currentType].list;
    },
    isShowTabControl() {
      return;
    },
  },

  created() {
    // 1.请求多个数据
    this.getMultiData();

    //2.请求商品数据
    this.getHomeProducts(POP);
    this.getHomeProducts(NEW);
    this.getHomeProducts(SELL);
  },
  mounted() {
    //3.监听GoodsListItem中图片加载完成，更新scroll可滚动长度
    const refresh = debounce(this.$refs.scroll.refresh, 500);
    this.$bus.$on("itemImageLoad", () => {
      refresh();
    });
  },
  destroyed() {},
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 500);
    this.$refs.scroll.refresh();
  },
  deactivated() {
    this.saveY = this.$refs.scroll.getScrollY();
  },
  methods: {
    //匹配不同的tabcontrol对应的商品类型type
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = POP;
          break;
        case 1:
          this.currentType = NEW;
          break;
        case 2:
          this.currentType = SELL;
          break;
      }
      this.$refs.tabControl2.currentIndex = index;
      this.$refs.tabControl1.currentIndex = index;
    },
    backClick() {
      this.$refs.scroll.scrollTo(0, 0, 500);
    },
    contentScroll(position) {
      // 1.决定tabFixed是否显示
      this.isTabFixed = position.y < -this.tabOffsetTop;
      //2.决定backtop是否显示
      this.isShowBackTop = Math.abs(position.y) > 1000;
    },
    loadMore() {
      this.getHomeProducts(this.currentType);
      // this.$refs.scroll.refresh();
    },
    /**
     * 网络请求相关方法
     */
    //获取轮播图、功能视图、推荐栏的信息
    getMultiData() {
      getHomeMultidata().then((res) => {
        //获取轮播图数据
        // console.log(res);
        this.banner = res.data[BANNER].list;
        this.recommends = res.data[RECOMMEND].list;
        // 下次更新DOM时,获取新的tabOffsetTop值(不保险,可以在updated钩子中获取)
        this.$nextTick(() => {
          this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
        });
      });
    },

    //获取商品列表数据
    getHomeProducts(type) {
      getHomeData(type, this.goodsList[type].page).then((res) => {
        const goodsList = res.data.list;
        this.goodsList[type].list.push(...goodsList);
        this.goodsList[type].page += 1;

        this.$refs.scroll.finishPullUp();
      });
    },
  },
};
</script>

<style scoped>
#home {
  position: relative;
  height: 100vh;
}
/* .content {
  height: calc(100% - 93px);
  overflow: hidden;
} */
.content {
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
  overflow: hidden;
}
.tab-control {
  position: relative;
}
.nav-bar {
  background-color: var(--color-tint);
  font-weight: 700;
  color: #fff;
}
</style>